<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\News;

class ProcessNews implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $fileName = 'small.json';

        $filePath = resource_path('data/' . $fileName);

        $label = [
            '产品'=> '1',
            '2' => '人事',
            '3' => '政策',
            '4' => '',
            '5' => 
        ]

        if (file_exists($filePath)) {
            $file = file($filePath);

            for ($i=0; $i < count($file); $i++) { 
                $line = json_decode($file[$i], true);

                $news = new News;
                if ($line['news_title']) {
                    $news->title = $line['news_title'];
                }

                if ($line['news_content']) {
                    $news->title = $line['news_content'];
                }

                if ($line['label']) {
                    $news->label = $this->getLabel($line['label']);
                }

                if ($line['company']) {
                    $news->company = $line['company'];
                }

                if ($line['people']) {
                    emplode(",", $line['people']);
                }
            }
        }

        echo "Finish process company" . PHP_EOL;
    }

    private getLabel($labelName) {
        if (labelName == "产品") {
            return 0;
        } else if (labelName == "人事") {
            return 1;
        } else if (labelName == "政策") {
            return 2;
        } else if (labelName == "数据") {
            return 3;
        } else if (labelName == "访谈") {
            return 4;
        } else if (labelName == "资本") {
            return 5;
        }
    }
}
